<template>
  <div class="app-container">
    <div style="margin-bottom: 20px;">
      <el-input v-model="listQuery.keyWord" placeholder="请输入设施名称" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-show="checkPermission('facilityLabelAdd')" v-waves class="filter-item" type="primary" icon="el-icon-plus" style="float:right" @click="handleAdd">
        添加设施
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :element-loading-text="g.loadingStr"
      :header-cell-style="{background:g.mainColor}"
    >
      <el-table-column label="序号" align="center" width="100" fixed>
        <template slot-scope="{$index}">
          <span>{{ $index + 1 + ( listQuery.currentPage - 1 ) * listQuery.pageSize }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center">
        <template slot-scope="{row}">
          <span><img :src="row.icon" alt="" class="table-row-img"></span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="{row}">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" width="90px">
        <template slot-scope="{row,$index}">
          <span>
            <el-switch
              v-model="row.status"
              @change="changeStatus(row,$index)"
            />
          </span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="160px">
        <template slot-scope="{row,$index}">
          <el-button v-show="checkPermission('facilityLabelEdit')" type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDel(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { facilityList, facilityEdit, labelDel } from '@/api/system'

export default {
  name: 'FacilityLabelList',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        keyWord: '',
        type: 1
      }
    }
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      facilityList(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.records
        for (const index in this.list) {
          this.list[index]['status'] = this.list[index]['status'] === 1
        }
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    handleUpdate(row) {
      console.log(row)
      this.$router.push({ name: 'FacilityLabelEdit', params: { id: row.id }})
    },
    handleAdd() {
      this.$router.push('FacilityLabelAdd')
    },
    handleDel(row, index) {
      console.log(row)

      this.$confirm('是否确认删除该标签？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(JSON.stringify(this.multipleSelection))
        labelDel({
          id: row['id']
        }).then(response => {
          this.$message({ message: '操作成功', type: 'success' })
          this.list.splice(index, 1)
        })
      })
    },
    changeStatus(row, index) {
      console.log(row)
      console.log(index)
      facilityEdit({
        id: row['id'],
        status: row['status'] ? 1 : 0
      }).then(response => {
        this.$message({ message: '操作成功', type: 'success' })
      }).catch(() => {
        this.list[index]['status'] = !this.list[index]['status']
      })
    }
  }
}
</script>
